package com.liu.spider.component;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpStatus;
import com.liu.spider.proxy.spider.Ip66Spider;
import com.liu.spider.proxy.spider.KuaidailiSpider;
import com.liu.spider.service.ProxyService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Component("ProxyValidTask")
@Slf4j
public class ProxyValidTask {

    private ExecutorService workerGroup = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2);
    @Autowired
    ProxyService proxyService;

    public void taskNoParams(){
        int pageSize = 200;
        long count = this.proxyService.count();
        log.info("===>>>当前代理池剩余[{}]个代理", count);

        for (long start = 0; start < count; start += pageSize) {
            long end = Long.min(start + pageSize - 1, count);
            Set<Object> results = this.proxyService.batchQuery(start, end);

            if (!CollectionUtils.isEmpty(results)) {
                for (Object proxy : results) {
                    workerGroup.submit(() -> {
                        log.info(proxy.toString());
                        String[] addrArr = proxy.toString().split(":");
                        String ip = addrArr[0];
                        int port = Integer.parseInt(addrArr[1]);

                        try {
                            HttpRequest request = new HttpRequest("http://httpbin.org/get");
                            Proxy httpProxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ip, port));
                            request.setProxy(httpProxy);
                            HttpResponse response = request.timeout(5000).executeAsync();

                            if (response.getStatus() == HttpStatus.HTTP_OK) {
                                this.proxyService.validOk(proxy.toString());
                            } else {
                                this.proxyService.decrementOrRemove(proxy.toString());
                            }
                        } catch (Exception e) {
                            log.warn("===>>>代理[{}]验证失败", proxy);
                            this.proxyService.decrementOrRemove(proxy.toString());
                        }
                    });
                }
            }

        }
    }

}
